package com.bayleaf.web.sql;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

import com.bayleaf.common.StringUtil;
import com.bayleaf.core.BayleafServlet;
import com.bayleaf.core.Model;
import com.bayleaf.web.sample.SampleVO;


@WebServlet(name="sqlGen",
urlPatterns={"/sqlGen"},
initParams={ @WebInitParam(name="title", value="SQL Generator"), @WebInitParam(name="jsp", value="/sqlGen/SqlGen") } )
public class SqlGen extends BayleafServlet {

	private static final String LineSep = System.getProperty( "line.separator" );


	@Override
	public String process(Model model) throws Exception {
		// TODO Auto-generated method stub
		
		String input = model.getString("input");
    	
    	System.out.println("input data : "+input);
    	
    	
//    	SampleVO vo = new SampleVO();
//    	
//    	vo.setOutput(output);
    	String fields[] = StringUtil.strTokenizer(input);
    	model.setString("insert",genInsertStatement(fields));
    	model.setString("select", genSelectStatement(fields));
    	model.setString("update", genUpdateStatement(fields));
    	model.setString("remove", genDeleteStatement(fields));
    	
		
		return null;
	}
	
	/**
	 * SELECT 구문을 생성한다.
	 * @param fields
	 * @return
	 */
	public String genSelectStatement(String[] fields){
		StringBuilder result = new StringBuilder();
		result.append("SELECT " + LineSep);
		for(int i=0; i < fields.length; i ++){
			result.append((i==0?"":",") + fields[i] + LineSep); 
    	}
		result.append("FROM TAB1" +LineSep);
		result.append("WHERE " +LineSep);
		for(int i=0; i < fields.length; i ++){
			result.append((i==0?"":"AND ")+fields[i]+ "="+":"+ StringUtil.underScoreToCamelCase(fields[i])+ LineSep); 
		}
		return result.toString();
	}
	
	/**
	 * INSERT 구문을 생성한다.
	 * @param fields
	 * @return
	 */
	public String genInsertStatement(String[] fields){
		StringBuilder result = new StringBuilder();
		result.append("INSERT " + LineSep);
		result.append("INTO TAB1_NM" +LineSep);
		result.append("(" +LineSep);
		
		for(int i=0; i < fields.length; i ++){
			result.append((i==0?"":",") + fields[i] + LineSep); 
		}
		result.append(")" +LineSep);
		result.append(" VALUES " +LineSep);
		result.append("(" +LineSep);
		for(int i=0; i < fields.length; i ++){
			result.append((i==0?"":",")+":"+ StringUtil.underScoreToCamelCase(fields[i]) +" /*"+fields[i]+"*/"+LineSep); 
		}
		result.append(")");
		return result.toString();
	}
	
	/**
	 * UPDATE 구문을 생성한다.
	 * @param fields
	 * @return
	 */
	public String genUpdateStatement(String[] fields){
		StringBuilder result = new StringBuilder();
		result.append("UPDATE TAB1_NM" + LineSep);
		result.append("SET" +LineSep);
		for(int i=0; i < fields.length; i ++){
			result.append((i==0?"":",") + fields[i] +"="+":"+ StringUtil.underScoreToCamelCase(fields[i])+ LineSep); 
		}
		result.append("WHERE " +LineSep);
		for(int i=0; i < fields.length; i ++){
			result.append((i==0?"":"AND ")+fields[i]+ "="+":"+ StringUtil.underScoreToCamelCase(fields[i])+ LineSep); 
		}
		return result.toString();
	}
	
	
	/**
	 * DELETE 구문을 생성한다.
	 * @param fields
	 * @return
	 */
	public String genDeleteStatement(String[] fields){
		StringBuilder result = new StringBuilder();
		result.append("DELETE" + LineSep
				+"FROM TAB1_NM" + LineSep);
		result.append("WHERE " +LineSep);
		for(int i=0; i < fields.length; i ++){
			result.append((i==0?"":"AND ")+fields[i]+ "="+ ":"+ StringUtil.underScoreToCamelCase(fields[i])+LineSep); 
		}
		return result.toString();
	}
	

}
